/**
 * 
 */
package travibot.engine.tasks;

import java.text.Format;
import java.util.Date;

import org.apache.log4j.Logger;

import travibot.connection.ConnectionFactory;
import travibot.engine.ScheduledTask;
import travibot.engine.Task;
import travibot.model.Structure;
import travibot.model.Village;
import travibot.resource.ResourceHelper;
import travibot.utility.RequestDelayGenarator;


/**
 * @author macekpet
 *
 */
public class StructureUpgradeTask implements ScheduledTask {

	private static Logger log = Logger.getLogger(StructureUpgradeTask.class);
	
    private ConnectionFactory connectionFactory;
    private Date scheduledDate;
    private Structure structure;
    private Village village;
    
    

    /* (non-Javadoc)
     * @see java.lang.Comparable#compareTo(java.lang.Object)
     */
    public int compareTo(Task obj) {

        int ret = 0;
        if(obj instanceof ScheduledTask){
            ScheduledTask other = (ScheduledTask)obj;
            ret = scheduledDate.compareTo(other.getScheduledTime());
        } else {
            ret = -1;
        }
        return ret;
    }

    /* (non-Javadoc)
     * @see travibot.engine.Task#execute()
     */
    public ExecutionResult execute() throws InterruptedException {
    	try {
    		String link = structure.getUpgradeLink(village.getId());    	
    		connectionFactory.getInputStream(link);
    		return ExecutionResult.SUCCESS;
    	} catch (Exception e){
    		log.error(e);
    		return ExecutionResult.FAILURE;
    	}
    }

    /* (non-Javadoc)
     * @see travibot.engine.ScheduledTask#getScheduledTime()
     */
    public Date getScheduledTime() {

        return scheduledDate;
    }

    /* (non-Javadoc)
     * @see travibot.engine.Task#getType()
     */
    public Type getType() {

        return Task.Type.SCHEDULED_TASK;
    }

    
    public void setConnectionFactory(ConnectionFactory connectionFactory) {
    
        this.connectionFactory = connectionFactory;
    }

    
    public void setScheduledDate(Date scheduledDate) {
    
        this.scheduledDate = scheduledDate;
    }

    
    public void setStructure(Structure structure) {
    
        this.structure = structure;
    }

    
    public void setVillage(Village village) {
    
        this.village = village;
    }
    
    

}
